<template>
  <!--        title-->
  <!--        kindList-->
  <!--        sizeList-->
  <!--        quickList-->
  <!--        textureList-->
  <!--        systemList、筛选]【修改、删除】-->
  <div style="margin-left: 10px">
    <strong style="display: inline-block; margin: 0 0 10px; ">自行车管理</strong>
    <!-- 搜索组件-->
    <div class="search">
      <searchBar @search="search">
        <!-- 添加自行车slot -->
        <span @click="addBike">自行车添加</span>
      </searchBar>
    </div>
    <!--        添加自行车组件-->
    <addBike
      :addBikeVisible="addBikeVisible"
      :isEdit="isEdit"
      @closeAddBike="closeAddBike"
      ref="addEditDialog"
    ></addBike>
    <!--  自行车列表[通过自行车-->
    <div class="bike-list">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="title" label="名称" width="200"></el-table-column>
        <el-table-column prop="kind" label="类别" ></el-table-column>
        <el-table-column prop="size" label="尺寸" ></el-table-column>
        <el-table-column prop="quick" label="速别"></el-table-column>
        <el-table-column prop="texture" label="材质"></el-table-column>
        <el-table-column prop="system" label="系统"></el-table-column>
        <el-table-column prop="integral" label="积分"></el-table-column>
        <el-table-column prop="count" label="库存"></el-table-column>
        <el-table-column prop="action" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle @click="editBike(scope.row)"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteBike(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { bikeList, delBike } from "../../../api";

import { kindList } from "../../../data";
import searchBar from "../../../components/searchBar/searchBar";
import addBike from "../../../components/admin/addBike/addBike";

export default {
  name: "bikeManage",
  data() {
    return {
      kindList: kindList,
      addBikeVisible: false,
      isEdit: false,
      tableData: []
    };
  },
  components: {
    searchBar,
    addBike
  },
  mounted() {
    this.getBikeList();
  },
  methods: {
    getBikeList(e) {
      bikeList(e).then(res => {
        this.tableData = res.result.bikeList;
        window.console.log(res, "bikeList");
      });
    },
    search(e) {
      // eslint-disable-next-line no-console
      console.log(e, "e");
      this.getBikeList(e)
    },
    addBike() {
      this.addBikeVisible = true;
    },
    closeAddBike(e) {
      this.addBikeVisible = e;
      this.isEdit = e;
      this.getBikeList();
    },
    editBike(editData) {
      let editDataObj = JSON.parse(JSON.stringify(editData));
      // eslint-disable-next-line no-console
      console.log(editDataObj, "editData");
      this.isEdit = true;
      this.addBikeVisible = true;

      let {
        id,
        adminId,
        title,
        intro,
        kind,
        size,
        quick,
        texture,
        system,
        integral,
        count
      } = editDataObj;
      this.$refs.addEditDialog.form = {
        bikeId: id,
        adminId,
        title,
        intro,
        kind,
        size,
        quick,
        texture,
        system,
        integral,
        count
      };
      // eslint-disable-next-line no-console
      console.log(this.$refs.addEditDialog, "addEditDialog");
    },
    deleteBike(id) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delBike({ bikeId: id }).then(res => {
            if (res.code === 200) {
              this.$message({
                message: res.data,
                type: "success"
              });
              this.getBikeList()
            } else {
              this.$message({
                message: res.data,
                type: "错误"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped lang="stylus" ref="stylesheet/stylus"></style>
